import requests
import re

headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"}

item_url = 'https://www.hdbz.net/'
item_response = requests.get(item_url)
# print(item_response.content.decode('utf-8'))
item_response_re = re.findall(r'<a href="(/wallpaper/\d{6}/\d{6}.html)" target="_blank".*?">.*?img.*?data-original="(.*?)\?x-oss-process=style/w_auto-h_350" alt="(.*?)" class="lazy" />',item_response.content.decode('utf-8'),re.S)
# print(item_response_re)

for item in item_response_re:
    link = item[0]
    shrinkage_url = item[1]
    title = item[2]

    #  保存缩裂图
    # request_shrinkage = requests.get(shrinkage_url)
    # with open(f'../tu/高清壁纸/s/{title}.jpg','wb') as f:
    #     f.write(request_shrinkage.content)
    #得到详情信息
    link_url = f"https://www.hdbz.net{link}"

    response_link = requests.get(link_url,headers=headers)
    print(response_link.content.decode('utf-8'))
    response_link_re = re.findall(r'<div class="pic-preview">.*?<img alt="(.*?)" src="(.*?)\?x-oss-process=style/w_1330-h_870" />.*?</div>',response_link.content.decode('utf-8'),re.S)
    for link in response_link_re:
        # 预览图地址 和名称
        # print(link)
        title = link[0]
        preview_url =link[1]
        # print(title)
        # print(preview_url)

        img = requests.get(preview_url)
        print(img.content)
        with open(f'../tu/高清壁纸/y/{title}.jpg','wb') as f:
            f.write(img.content)
    break






